package com.serkol.abmanager.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.bean.Result;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Automobiles;
import com.serkol.abmanager.entity.Departments;
import com.serkol.abmanager.entity.Mechanisms;
import com.serkol.abmanager.service.AutomobilesService;
import com.serkol.abmanager.service.MechanismsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/mechanisms")
@Api(tags = "机构表")
@ResponseBody
public class MechanismsApiController {

    @Autowired
    private MechanismsService service;
@ApiOperation(value = "分页查询")
    @PostMapping("/findByList")
    public GridResult findByList(@RequestParam(name = "name",defaultValue = "") String name,@RequestParam(name = "page",defaultValue = "0") int page, @RequestParam(name = "pagesize",defaultValue = "10") int pagesize){

        GridResult page1 = service.findByList(name,page,pagesize);

        return  page1;
    }
    @ApiOperation(value = "机构查询")
    @PostMapping("/findById")
    public Result findById(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            Mechanisms byId = service.getById(id);
            if (byId != null) {
                return Result.ok(byId);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }

    @ApiOperation(value = "删除")
    @PostMapping("/findByDelete")
    public Result findByDelete(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            boolean b = service.removeById(id);
            if (b) {
                return Result.ok();
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }
    @ApiOperation(value = "添加或编辑")
    @PostMapping("/edit")
    public Result edit(@RequestParam(name = "id", defaultValue = "0") long id,
                       @RequestParam(name = "type", defaultValue = "0") int type,
                       @RequestParam(name = "name", defaultValue = "") String name,
                       @RequestParam(name = "legalPerson", defaultValue = "") String legalPerson,
                       @RequestParam(name = "address", defaultValue = "") String address,
                       @RequestParam(name = "region", defaultValue = "") String region,
                       @RequestParam(name = "tell", defaultValue = "") String tell)
    {
        Result result = new Result();
        if (!legalPerson.isEmpty() && !address.isEmpty() && !region.isEmpty() && !tell.isEmpty()  && !name.isEmpty()) {
            Mechanisms goods=new Mechanisms();
            goods.setLegalPerson(legalPerson);
            goods.setAddress(address);
            goods.setRegion(region);
            goods.setName(name);
            goods.setTell(tell);
            goods.setType(type);
            boolean flag = false;
            if (id > 0) {
                goods.setId(id);
                flag = service.updateById(goods);
            } else {
                flag = service.save(goods);
            }
            if (flag) {
                result.setStatus(ResultConstant.SUCSESS);
            } else {
                result.setStatus(ResultConstant.FAIL);
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }
}
